home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / copy.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  12KB  |  490 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Generic (shallow and deep) copying operations.
  5.  
  6. Interface summary:
  7.  
  8.         import copy
  9.  
  10.         x = copy.copy(y)        # make a shallow copy of y
  11.         x = copy.deepcopy(y)    # make a deep copy of y
  12.  
  13. For module specific errors, copy.Error is raised.
  14.  
  15. The difference between shallow and deep copying is only relevant for
  16. compound objects (objects that contain other objects, like lists or
  17. class instances).
  18.  
  19. - A shallow copy constructs a new compound object and then (to the
  20.   extent possible) inserts *the same objects* into it that the
  21.   original contains.
  22.  
  23. - A deep copy constructs a new compound object and then, recursively,
  24.   inserts *copies* into it of the objects found in the original.
  25.  
  26. Two problems often exist with deep copy operations that don\'t exist
  27. with shallow copy operations:
  28.  
  29.  a) recursive objects (compound objects that, directly or indirectly,
  30.     contain a reference to themselves) may cause a recursive loop
  31.  
  32.  b) because deep copy copies *everything* it may copy too much, e.g.
  33.     administrative data structures that should be shared even between
  34.     copies
  35.  
  36. Python\'s deep copy operation avoids these problems by:
  37.  
  38.  a) keeping a table of objects already copied during the current
  39.     copying pass
  40.  
  41.  b) letting user-defined classes override the copying operation or the
  42.     set of components copied
  43.  
  44. This version does not copy types like module, class, function, method,
  45. nor stack trace, stack frame, nor file, socket, window, nor array, nor
  46. any similar types.
  47.  
  48. Classes can use the same interfaces to control copying that they use
  49. to control pickling: they can define methods called __getinitargs__(),
  50. __getstate__() and __setstate__().  See the documentation for module
  51. "pickle" for information on these methods.
  52. '''
  53. import types
  54. import weakref
  55. from copy_reg import dispatch_table
  56.  
  57. class Error(Exception):
  58.     pass
  59.  
  60. error = Error
  61.  
  62. try:
  63.     from org.python.core import PyStringMap
  64. except ImportError:
  65.     PyStringMap = None
  66.  
  67. __all__ = [
  68.     'Error',
  69.     'copy',
  70.     'deepcopy']
  71.  
  72. def copy(x):
  73.     """Shallow copy operation on arbitrary Python objects.
  74.  
  75.     See the module's __doc__ string for more info.
  76.     """
  77.     cls = type(x)
  78.     copier = _copy_dispatch.get(cls)
  79.     if copier:
  80.         return copier(x)
  81.     copier = None(cls, '__copy__', None)
  82.     if copier:
  83.         return copier(x)
  84.     reductor = None.get(cls)
  85.     if reductor:
  86.         rv = reductor(x)
  87.     else:
  88.         reductor = getattr(x, '__reduce_ex__', None)
  89.         if reductor:
  90.             rv = reductor(2)
  91.         else:
  92.             reductor = getattr(x, '__reduce__', None)
  93.             if reductor:
  94.                 rv = reductor()
  95.             else:
  96.                 raise Error('un(shallow)copyable object of type %s' % cls)
  97.             return None(x, rv, 0)
  98.  
  99. _copy_dispatch = d = { }
  100.  
  101. def _copy_immutable(x):
  102.     return x
  103.  
  104. for t in (type(None), int, long, float, bool, str, tuple, frozenset, type, xrange, types.ClassType, types.BuiltinFunctionType, type(Ellipsis), types.FunctionType, weakref.ref):
  105.     d[t] = _copy_immutable
  106.  
  107. for name in ('ComplexType', 'UnicodeType', 'CodeType'):
  108.     t = getattr(types, name, None)
  109.     if t is not None:
  110.         d[t] = _copy_immutable
  111.         continue
  112.  
  113. def _copy_with_constructor(x):
  114.     return type(x)(x)
  115.  
  116. for t in (list, dict, set):
  117.     d[t] = _copy_with_constructor
  118.  
  119.  
  120. def _copy_with_copy_method(x):
  121.     return x.copy()
  122.  
  123. if PyStringMap is not None:
  124.     d[PyStringMap] = _copy_with_copy_method
  125.  
  126. def _copy_inst(x):
  127.     if hasattr(x, '__copy__'):
  128.         return x.__copy__()
  129.     if None(x, '__getinitargs__'):
  130.         args = x.__getinitargs__()
  131.         y = x.__class__(*args)
  132.     else:
  133.         y = _EmptyClass()
  134.         y.__class__ = x.__class__
  135.     if hasattr(x, '__getstate__'):
  136.         state = x.__getstate__()
  137.     else:
  138.         state = x.__dict__
  139.     if hasattr(y, '__setstate__'):
  140.         y.__setstate__(state)
  141.     else:
  142.         y.__dict__.update(state)
  143.     return y
  144.  
  145. d[types.InstanceType] = _copy_inst
  146. del d
  147.  
  148. def deepcopy(x, memo = None, _nil = []):
  149.     """Deep copy operation on arbitrary Python objects.
  150.  
  151.     See the module's __doc__ string for more info.
  152.     """
  153.     if memo is None:
  154.         memo = { }
  155.     d = id(x)
  156.     y = memo.get(d, _nil)
  157.     if y is not _nil:
  158.         return y
  159.     cls = None(x)
  160.     copier = _deepcopy_dispatch.get(cls)
  161.     if copier:
  162.         y = copier(x, memo)
  163.     else:
  164.         
  165.         try:
  166.             issc = issubclass(cls, type)
  167.         except TypeError:
  168.             issc = 0
  169.  
  170.         if issc:
  171.             y = _deepcopy_atomic(x, memo)
  172.         else:
  173.             copier = getattr(x, '__deepcopy__', None)
  174.             if copier:
  175.                 y = copier(memo)
  176.             else:
  177.                 reductor = dispatch_table.get(cls)
  178.                 if reductor:
  179.                     rv = reductor(x)
  180.                 else:
  181.                     reductor = getattr(x, '__reduce_ex__', None)
  182.                     if reductor:
  183.                         rv = reductor(2)
  184.                     else:
  185.                         reductor = getattr(x, '__reduce__', None)
  186.                         if reductor:
  187.                             rv = reductor()
  188.                         else:
  189.                             raise Error('un(deep)copyable object of type %s' % cls)
  190.                         y = None(x, rv, 1, memo)
  191.                         memo[d] = y
  192.                         _keep_alive(x, memo)
  193.                         return y
  194.  
  195. _deepcopy_dispatch = d = { }
  196.  
  197. def _deepcopy_atomic(x, memo):
  198.     return x
  199.  
  200. d[type(None)] = _deepcopy_atomic
  201. d[type(Ellipsis)] = _deepcopy_atomic
  202. d[int] = _deepcopy_atomic
  203. d[long] = _deepcopy_atomic
  204. d[float] = _deepcopy_atomic
  205. d[bool] = _deepcopy_atomic
  206.  
  207. try:
  208.     d[complex] = _deepcopy_atomic
  209. except NameError:
  210.     pass
  211.  
  212. d[str] = _deepcopy_atomic
  213.  
  214. try:
  215.     d[unicode] = _deepcopy_atomic
  216. except NameError:
  217.     pass
  218.  
  219.  
  220. try:
  221.     d[types.CodeType] = _deepcopy_atomic
  222. except AttributeError:
  223.     pass
  224.  
  225. d[type] = _deepcopy_atomic
  226. d[xrange] = _deepcopy_atomic
  227. d[types.ClassType] = _deepcopy_atomic
  228. d[types.BuiltinFunctionType] = _deepcopy_atomic
  229. d[types.FunctionType] = _deepcopy_atomic
  230. d[weakref.ref] = _deepcopy_atomic
  231.  
  232. def _deepcopy_list(x, memo):
  233.     y = []
  234.     memo[id(x)] = y
  235.     for a in x:
  236.         y.append(deepcopy(a, memo))
  237.     
  238.     return y
  239.  
  240. d[list] = _deepcopy_list
  241.  
  242. def _deepcopy_tuple(x, memo):
  243.     y = []
  244.     for a in x:
  245.         y.append(deepcopy(a, memo))
  246.     
  247.     d = id(x)
  248.     
  249.     try:
  250.         return memo[d]
  251.     except KeyError:
  252.         pass
  253.  
  254.     for i in range(len(x)):
  255.         if x[i] is not y[i]:
  256.             y = tuple(y)
  257.             break
  258.             continue
  259.         y = x
  260.         memo[d] = y
  261.         return y
  262.  
  263. d[tuple] = _deepcopy_tuple
  264.  
  265. def _deepcopy_dict(x, memo):
  266.     y = { }
  267.     memo[id(x)] = y
  268.     for key, value in x.iteritems():
  269.         y[deepcopy(key, memo)] = deepcopy(value, memo)
  270.     
  271.     return y
  272.  
  273. d[dict] = _deepcopy_dict
  274. if PyStringMap is not None:
  275.     d[PyStringMap] = _deepcopy_dict
  276.  
  277. def _deepcopy_method(x, memo):
  278.     return type(x)(x.im_func, deepcopy(x.im_self, memo), x.im_class)
  279.  
  280. _deepcopy_dispatch[types.MethodType] = _deepcopy_method
  281.  
  282. def _keep_alive(x, memo):
  283.     '''Keeps a reference to the object x in the memo.
  284.  
  285.     Because we remember objects by their id, we have
  286.     to assure that possibly temporary objects are kept
  287.     alive by referencing them.
  288.     We store a reference at the id of the memo, which should
  289.     normally not be used unless someone tries to deepcopy
  290.     the memo itself...
  291.     '''
  292.     
  293.     try:
  294.         memo[id(memo)].append(x)
  295.     except KeyError:
  296.         memo[id(memo)] = [
  297.             x]
  298.  
  299.  
  300.  
  301. def _deepcopy_inst(x, memo):
  302.     if hasattr(x, '__deepcopy__'):
  303.         return x.__deepcopy__(memo)
  304.     if None(x, '__getinitargs__'):
  305.         args = x.__getinitargs__()
  306.         args = deepcopy(args, memo)
  307.         y = x.__class__(*args)
  308.     else:
  309.         y = _EmptyClass()
  310.         y.__class__ = x.__class__
  311.     memo[id(x)] = y
  312.     if hasattr(x, '__getstate__'):
  313.         state = x.__getstate__()
  314.     else:
  315.         state = x.__dict__
  316.     state = deepcopy(state, memo)
  317.     if hasattr(y, '__setstate__'):
  318.         y.__setstate__(state)
  319.     else:
  320.         y.__dict__.update(state)
  321.     return y
  322.  
  323. d[types.InstanceType] = _deepcopy_inst
  324.  
  325. def _reconstruct(x, info, deep, memo = None):
  326.     if isinstance(info, str):
  327.         return x
  328.     if not None(info, tuple):
  329.         raise AssertionError
  330.     if None is None:
  331.         memo = { }
  332.     n = len(info)
  333.     if not n in (2, 3, 4, 5):
  334.         raise AssertionError
  335.     (callable, args) = None[:2]
  336.     if n > 2:
  337.         state = info[2]
  338.     else:
  339.         state = { }
  340.     if n > 3:
  341.         listiter = info[3]
  342.     else:
  343.         listiter = None
  344.     if n > 4:
  345.         dictiter = info[4]
  346.     else:
  347.         dictiter = None
  348.     if deep:
  349.         args = deepcopy(args, memo)
  350.     y = callable(*args)
  351.     memo[id(x)] = y
  352.     if state:
  353.         if deep:
  354.             state = deepcopy(state, memo)
  355.         if hasattr(y, '__setstate__'):
  356.             y.__setstate__(state)
  357.         elif isinstance(state, tuple) and len(state) == 2:
  358.             (state, slotstate) = state
  359.         else:
  360.             slotstate = None
  361.         if state is not None:
  362.             y.__dict__.update(state)
  363.         if slotstate is not None:
  364.             for key, value in slotstate.iteritems():
  365.                 setattr(y, key, value)
  366.             
  367.         
  368.     if listiter is not None:
  369.         for item in listiter:
  370.             if deep:
  371.                 item = deepcopy(item, memo)
  372.             y.append(item)
  373.         
  374.     if dictiter is not None:
  375.         for key, value in dictiter:
  376.             if deep:
  377.                 key = deepcopy(key, memo)
  378.                 value = deepcopy(value, memo)
  379.             y[key] = value
  380.         
  381.     return y
  382.  
  383. del d
  384. del types
  385.  
  386. class _EmptyClass:
  387.     pass
  388.  
  389.  
  390. def _test():
  391.     l = [
  392.         None,
  393.         1,
  394.         0x2L,
  395.         3.14,
  396.         'xyzzy',
  397.         (1, 0x2L),
  398.         [
  399.             3.14,
  400.             'abc'],
  401.         {
  402.             'abc': 'ABC' },
  403.         (),
  404.         [],
  405.         { }]
  406.     l1 = copy(l)
  407.     print l1 == l
  408.     l1 = map(copy, l)
  409.     print l1 == l
  410.     l1 = deepcopy(l)
  411.     print l1 == l
  412.     
  413.     class C:
  414.         
  415.         def __init__(self, arg = None):
  416.             self.a = 1
  417.             self.arg = arg
  418.             if __name__ == '__main__':
  419.                 import sys as sys
  420.                 file = sys.argv[0]
  421.             else:
  422.                 file = __file__
  423.             self.fp = open(file)
  424.             self.fp.close()
  425.  
  426.         
  427.         def __getstate__(self):
  428.             return {
  429.                 'a': self.a,
  430.                 'arg': self.arg }
  431.  
  432.         
  433.         def __setstate__(self, state):
  434.             for key, value in state.iteritems():
  435.                 setattr(self, key, value)
  436.             
  437.  
  438.         
  439.         def __deepcopy__(self, memo = None):
  440.             new = self.__class__(deepcopy(self.arg, memo))
  441.             new.a = self.a
  442.             return new
  443.  
  444.  
  445.     c = C('argument sketch')
  446.     l.append(c)
  447.     l2 = copy(l)
  448.     print l == l2
  449.     print l
  450.     print l2
  451.     l2 = deepcopy(l)
  452.     print l == l2
  453.     print l
  454.     print l2
  455.     l.append({
  456.         l[1]: l,
  457.         'xyz': l[2] })
  458.     l3 = copy(l)
  459.     import repr as repr
  460.     print map(repr.repr, l)
  461.     print map(repr.repr, l1)
  462.     print map(repr.repr, l2)
  463.     print map(repr.repr, l3)
  464.     l3 = deepcopy(l)
  465.     import repr as repr
  466.     print map(repr.repr, l)
  467.     print map(repr.repr, l1)
  468.     print map(repr.repr, l2)
  469.     print map(repr.repr, l3)
  470.     
  471.     class odict(dict):
  472.         
  473.         def __init__(self, d = { }):
  474.             self.a = 99
  475.             dict.__init__(self, d)
  476.  
  477.         
  478.         def __setitem__(self, k, i):
  479.             dict.__setitem__(self, k, i)
  480.             self.a
  481.  
  482.  
  483.     o = odict({
  484.         'A': 'B' })
  485.     x = deepcopy(o)
  486.     print (o, x)
  487.  
  488. if __name__ == '__main__':
  489.     _test()
  490.